package com.yiguantong.police;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.yiguantong.police.R;
import com.yiguantong.police.toolbar.ToolbarMenuEnum;
import com.yiguantong.police.ui.activity.ComBaseActivity;
import com.yiguantong.police.ui.dialog.DialogUtil;
import com.yiguantong.police.ui.utils.PreventHackingUtil;

import androidx.appcompat.widget.Toolbar;

/**
 * @description: 基类Activity
 * @date: 2017/9/19
 * @version:
 */
public class BaseActivity extends ComBaseActivity {

    protected Toolbar toolbar;
    protected TextView toolBarTitle;
    protected int PERMISSIONS_RESULT_CODE=1;

    @Override
    protected void onResume() {
        super.onResume();
        PreventHackingUtil.getinstance().onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        PreventHackingUtil.getinstance().onPause(this);
    }

    /**
     * 设置通用toolbar
     *
     * @param title
     * @param backIconVisible
     */
    protected void initToolbar(final String title, boolean backIconVisible,final Toolbar.OnClickListener backListener) {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolBarTitle = (TextView) findViewById(R.id.toolbar_title);
        if (toolbar == null)
            return;
        toolbar.setTitle("");
        if (toolBarTitle != null) {
            toolBarTitle.setText(title);
        }
        setSupportActionBar(toolbar);
        if (backIconVisible) {
            toolbar.setNavigationIcon(R.mipmap.icon_title_back);
        } else {
            toolbar.setNavigationIcon(null);
        }

        toolbar.setNavigationOnClickListener((new Toolbar.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (backListener != null) {
                    backListener.onClick(v);
                } else {
                    finish();
                }
            }
        }));

        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.refresh:
                        onMenuClick(item);
                        break;
                }
                return true;
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar_menu, menu);//加载menu文件到布局
        MenuItem refreshItem = menu.findItem(R.id.refresh);
        refreshItem.setVisible(false);
        if (refreshItem !=null){
            refreshItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
            onMenuCreatedComplete(refreshItem);
        }
        return true;
    }

    /**
     * 默认处理Toolbar(设置标题，返回键关闭Activity,不显示menu)
     *
     * @param title 标题
     * @param popupIcon
     */
    protected void initToolbar(String title, ToolbarMenuEnum popupIcon) {
        this.initToolbar(title, true,null);
    }

    /**
     * setting toolbar title
     */
    protected void setToolBarTitle(String title) {
        if (toolBarTitle != null && title != null) {
            toolBarTitle.setText("" + title);
        }
    }

    /**
     * setMenu Text when menu created
     */
    protected void onMenuCreatedComplete(MenuItem menuItem) {
    }

    protected void onMenuClick(MenuItem menuItem) {
    }

    /**
     * 拒绝权限后弹出的弹窗
     *
     * @param details   内容
     * @param leftText  左侧按钮
     * @param rightText 右侧按钮
     * @param title     标题
     */
    protected void showPermissionDialog(String details, String leftText, String rightText, String title) {
        final DialogUtil.DialogParams params = new DialogUtil.DialogParams(
                details, leftText, rightText, title);

        //取消弹窗
        params.setCancelListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                params.dismiss();
            }
        });
        //去开启
        params.setConfirmListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                params.dismiss();
                // 去设置
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", BaseActivity.this.getPackageName(), null);
                intent.setData(uri);
                startActivityForResult(intent,PERMISSIONS_RESULT_CODE);
            }
        });
        //返回键
        params.setKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    params.dismiss();
                    return true;
                }
                return false;
            }
        });
        DialogUtil.showPermissionDialog(BaseActivity.this, params).show();
    }
}
